<template>
  <ContainerGroup
    class="dept-container"
    style="flex-direction: row; box-sizing: border-box"
  >
    <!-- 规则名称列表区域开始 -->
    <Container class="dept-type" :height="height">
      <div class="title">
        规则名称
        <div class="add-rules">
          <Button
            style="padding: 4px 8px; font-size: 10px"
            class="self-set-btn"
            icon="addOutline"
            @click="addRule"
          >添加规则</Button>
        </div>
      </div>
      <el-table
        ref="ruleTable"
        :data="deptType"
        :show-header="false"
        highlight-current-row
        @row-click="rowClick"
      >
        <el-table-column align="left" prop="edName" label="名称">
          <template slot-scope="scope">
            <span style="margin-left: 12px">{{ scope.row.edName }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="90">
          <template slot-scope="scope">
            <div class="package">
              <span
                style="margin-right: 12px; flex: 0 0 30px"
                :class="{ 'is-default': scope.row.edExtraFirstCode === '1' }"
              >默认</span>
              <div
                class="set-default"
                :class="{
                  'current-default': scope.row.edExtraFirstCode === '1',
                }"
                @click.stop="setDefault(scope.row)"
              >
                <i class="el-icon-check" />
              </div>
              <span
                v-if="scope.row.edExtraFirstCode !== '1'"
                style="margin-right: 12px; flex: 0 0 30px"
                :class="{ 'is-default': scope.row.edExtraFirstCode !== '1','el-icon-delete':true }"
                @click="handleDeleteRl(scope.row)"
              />
            </div>
          </template>
        </el-table-column>
      </el-table>
    </Container>
    <!-- 规则名称列表区域结束 -->

    <!-- 右侧表格展示区域开始 -->
    <Container
      class="query-box"
      style="padding: 0; background: #f0f2f5"
      :height="height"
    >
      <!-- 查询 -->
      <Container style="margin-bottom: 12px">
        <div class="query-form">
          <FilterForm
            ref="filterForm"
            :elements="queryForm"
            @reset="reset"
            @submit="handleSearch"
          >
            <Button
              style="display: inline-block; margin-right: 10px"
              @click="handleExport"
            >导出</Button>
          </FilterForm>
        </div>
      </Container>

      <!-- 表格展示 -->
      <Container
        class="rule-detail"
        style="height: calc(100% - 67px); overflow: hidden"
      >
        <!-- 功能按钮 -->
        <div class="button-bar">
          <Button v-if="sureBtnBool" icon="add" @click="addDetail">新增</Button>
          <Button v-if="sureBtnBool" type="info" @click="handleJc">继承</Button>
        </div>

        <!-- 表格 -->
        <div class="table-box">
          <el-table
            v-loading="loading"
            :data="ruleDetail"
            default-expand-all
            border
            :header-cell-style="{
              background: '#fafafa',
              borderColor: '#e7ecf8',
            }"
            row-key="code"
            height="100%"
          >
            <!-- <el-table-column align="center" type="selection" width="55" /> -->
            <el-table-column
              align="center"
              type="index"
              label="序号"
              width="60"
            />
            <el-table-column prop="deptCode" label="部门编号" />
            <el-table-column prop="deptName" label="部门名称" />
            <el-table-column prop="pdeptCode" label="科室编号" />
            <el-table-column prop="pdeptName" label="科室名称" />
            <!-- <el-table-column prop="updatedOn" label="操作时间" width="180" /> -->
            <!-- <el-table-column v-if="sureBtnBool" align="center" label="是否启用" width="120">
              <template slot-scope="scope">
                <el-switch
                  v-if="scope.row.relationStatus === '1'"
                  v-model="scope.row.rowStatus"
                  active-color="#FFA712"
                  :active-value="'0'"
                  :inactive-value="'1'"
                  @change="switchUse(scope.row)"
                />
              </template>
            </el-table-column> -->
            <el-table-column v-if="sureBtnBool" align="center" label="操作" width="120">
              <template slot-scope="scope">
                <el-tooltip
                  v-if="scope.row.relationStatus === '1'"
                  class="item"
                  effect="dark"
                  content="编辑"
                  placement="top"
                >
                  <i
                    class="el-icon-edit-outline"
                    style="
                      padding-top: 4px;
                      font-size: 20px;
                      color: #207efa;
                      cursor: pointer;
                    "
                    @click="editDetail(scope.row)"
                  />
                </el-tooltip>
                <el-tooltip
                  v-if="scope.row.relationStatus === '1' && sureBtnBool"
                  class="item"
                  effect="dark"
                  content="删除"
                  placement="top"
                  style="margin-left: 10px"
                >
                  <i
                    class="el-icon-delete"
                    style="
                      padding-top: 4px;
                      font-size: 20px;
                      color: #207efa;
                      cursor: pointer;
                    "
                    @click="deleteDetail(scope.row)"
                  />
                </el-tooltip>
              </template>
            </el-table-column>
          </el-table>
        </div>

        <!-- 分页 -->
        <Pagination
          :total="pageAttr.total"
          :page="pageAttr.currentPage"
          :page-size="pageAttr.pageSize"
          @pagation="pagationChange"
        />
      </Container>
      <!-- 右侧表格展示区域结束 -->

      <!-- 对话框区域开始 -->
      <el-dialog
        :title="dialogTitle"
        :visible.sync="dialogVisible"
        width="40%"
        @close="dialogClose"
      >
        <el-form ref="form" :model="form" label-width="120px" size="mini">
          <el-form-item label="部门：" prop="deptCode">
            <el-select
              v-model="form.deptCode"
              :disabled="deptDisabled"
              filterable
              clearable
              style="width: 100%"
            >
              <el-option
                v-for="(item, index) in deptLists"
                :key="index"
                :value="item.deptCode + '/' + item.deptName"
                :label="item.deptName"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="所属科室：" prop="pdeptCode">
            <el-select v-model="form.pdeptCode" filterable clearable style="width: 100%">
              <el-option
                v-for="(item, index) in parentsList"
                :key="index"
                :value="item.deptCode + '/' + item.deptName"
                :label="item.deptName"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button size="mini" @click="dialogClose">取 消</el-button>
          <el-button
            type="primary"
            size="mini"
            @click="dialogCommit"
          >确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog
        :title="dialogTitle2"
        :visible.sync="dialogVisible2"
        width="40%"
      >
        <el-form ref="form2" :model="form2" label-width="120px">
          <el-form-item label="规则名称：" prop="ruleName">
            <el-input v-model="form2.ruleName" clearable style="width: 100%" />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button size="mini" @click="dialogClose2">取 消</el-button>
          <el-button
            type="primary"
            size="mini"
            @click="dialogCommit2"
          >确 定</el-button>
        </span>
      </el-dialog>
      <!-- 对话框区域结束 -->

      <!-- 弹层区域开始 -->
      <!-- <Drawer
        :visible="showEidtVlidate"
        :title="drawerTitle"
        width="30%"
        :footer-btns="[
          {
            text: '取消',
            type: 'info',
            trigger: 'cancel',
          },
          {
            text: '确定',
            type: 'primary',
            trigger: 'okBtn',
          },
        ]"
        @cancel="drawClose"
        @okBtn="saveEidtRule"
      >
        <el-form
          ref="form"
          :model="form"
          label-width="120px"
          style="margin-top:48px"
        >
          <el-form-item label="编号：">
            <el-input v-model="form.code" clearable style="width: auto" />
          </el-form-item>
          <el-form-item label="名称：">
            <el-input v-model="form.name" clearable style="width: auto" />
          </el-form-item>
          <el-form-item label="临床属性：">
            <el-select v-model="form.clinic" clearable>
              <template v-for="(item,index) in clinicList">
                <el-option v-if="item.name" :key="index" :value="item.value">{{ item.name }}</el-option>
              </template>
            </el-select>
          </el-form-item>
          <el-form-item label="科室属性：">
            <el-select v-model="form.dept" clearable>
              <template v-for="(item,index) in deptList">
                <el-option v-if="item.name" :key="index" :value="item.value">{{ item.name }}</el-option>
              </template>
            </el-select>
          </el-form-item>
          <el-form-item label="核算分类：">
            <el-select v-model="form.account" clearable>
              <template v-for="(item,index) in accountList">
                <el-option v-if="item.name" :key="index" :value="item.value">{{ item.name }}</el-option>
              </template>
            </el-select>
          </el-form-item>
          <el-form-item label="院区名称：">
            <el-select v-model="form.yard" clearable>
              <template v-for="(item,index) in yardList">
                <el-option v-if="item.name" :key="index" :value="item.value">{{ item.name }}</el-option>
              </template>
            </el-select>
          </el-form-item>
          <el-form-item label="所属上级：">
            <el-select v-model="form.parent" clearable>
              <template v-for="(item,index) in parentsList">
                <el-option v-if="item.name" :key="index" :value="item.value">{{ item.name }}</el-option>
              </template>
            </el-select>
          </el-form-item>
          <el-form-item label="是否往来账：">
            <el-radio-group v-model="form.isUse">
              <el-radio :label="'1'">是</el-radio>
              <el-radio :label="'0'">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </Drawer> -->
      <!-- 弹层区域结束 -->
      <!-- 继承 -->
      <Drawer
        :visible="jcVisable"
        title="继承"
        width="30%"
        :footer-btns="[
          {
            text: '取消',
            type: 'info',
            trigger: 'cancel',
          },
          {
            text: '确定',
            type: 'primary',
            trigger: 'okBtn',
          },
        ]"
        @cancel="() => (jcVisable = false)"
        @okBtn="handleSaveJc"
      >
        <el-form ref="form" label-width="120px" style="margin-top: 48px">
          <el-form-item label="源规则名称:" size="small">
            <el-select v-model="gzName" clearable>
              <template v-for="(item, index) in ruleList">
                <el-option
                  :key="index"
                  :value="item.edCode"
                  :label="item.edName"
                />
              </template>
            </el-select>
          </el-form-item>
        </el-form>
      </Drawer>
    </Container>
    <!-- 右侧表格展示区域结束 -->
  </ContainerGroup>
</template>

<script>
import {
  getDeptNameList,
  getParentCodeList,
  getCostDepartments,
  modifyOrganization,
  setDefault,
  getOrganizationSource,
  addEnumDetail,
  addOrganization,
  getNoRelationDepartmentList,
  getOrganizationNormal,
  batchDeleteOrganization,
  addInheritanceRules,
  downloadTemplate,
  deleteRule
} from '../../../utils/apis/accountingUnit/corres'
import { getEnumDetailSource } from '../../../utils/apis/basic'
import { BasicParams } from '../../../utils/basicParams'
import { BdOrganization } from '../../../utils/basicSetting/BdOrganization'

export default {
  // 核算对应
  name: 'Corres',
  components: {},
  data() {
    return {
      isDefault: '',
      ruleList: [],
      jcVisable: false,
      loading: false,
      dialogTitle: '',
      deptLists: [], // 所有部门
      dialogVisible: false,
      sureBtnBool: false,
      dialogTitle2: '',
      dialogVisible2: false,
      deptDisabled: false,
      // 分页参数
      pageAttr: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      // container高度
      height: 'fill',
      // 查询表单参数
      queryForm: [
        {
          label: '部门编码',
          key: 'deptCode',
          type: 'Select',
          clearable: true,
          value: '',
          filterable: true,
          remote: true,
          remoteMethod: this.unitCodeFilter,
          itemLabel: 'deptCode',
          itemValue: 'deptCode',
          options: []
        },
        {
          label: '部门名称',
          key: 'deptName',
          type: 'Select',
          clearable: true,
          value: '',
          filterable: true,
          remote: true,
          remoteMethod: this.unitFilter,
          itemLabel: 'deptName',
          itemValue: 'deptName',
          options: []
        },
        {
          label: '科室编码',
          key: 'pdeptCode',
          type: 'Select',
          clearable: true,
          value: '',
          filterable: true,
          remote: true,
          remoteMethod: this.deptCodeFilter,
          itemLabel: 'deptCode',
          itemValue: 'deptCode',
          options: []
        },
        {
          label: '科室名称',
          key: 'pdeptName',
          type: 'Select',
          clearable: true,
          value: '',
          filterable: true,
          remote: true,
          remoteMethod: this.deptFilter,
          itemLabel: 'deptName',
          itemValue: 'deptName',
          options: []
        },
        {
          label: '关联状态',
          key: 'relation',
          type: 'Select',
          clearable: true,
          value: '',
          itemLabel: 'label',
          itemValue: 'value',
          options: [
            {
              label: '全部',
              value: ''
            },
            {
              label: '关联',
              value: '1'
            },
            {
              label: '未关联',
              value: '0'
            }
          ]
        },
        {
          label: '启用状态',
          key: 'rowStatus',
          type: 'Select',
          clearable: true,
          value: '',
          itemLabel: 'label',
          itemValue: 'value',
          options: [
            {
              label: '全部',
              value: ''
            },
            {
              label: '启用',
              value: '0'
            },
            {
              label: '未启用',
              value: '1'
            }
          ]
        }

      ],
      // 会计分类
      deptType: [],
      // 规则详情表单
      ruleDetail: [],
      tableData: [],
      isEdit: false,
      showEidtVlidate: false,
      drawerTitle: '',
      // 新增或编辑表单
      form: {
        pdeptCode: '',
        deptCode: ''
      },
      form2: {
        ruleName: ''
      },
      clinicList: [
        {
          value: '1',
          name: '门诊'
        },
        {
          value: '2',
          name: '住院'
        },
        {
          value: '3',
          name: ''
        }
      ],
      accountList: [],
      deptList: [],
      yardList: [],
      parentsList: [],
      currentEdCode: '',
      edCodeFirst: '', // edcode
      currentRow: null,
      pubID: '',
      leftRow: {},
      newParamList: {},
      gzName: ''
    }
  },
  created() {
    this.init()
    this.deptFilterM()
  },
  mounted() {},
  methods: {
    handleDeleteRl(item) { // 规则删除
      this.$confirm({
        content: '是否确认删除？',
        title: '温馨提示',
        okFn: (res) => {
          const ids = {
            edId: item.edId,
            ruleCode: item.edCode
          }
          deleteRule(ids).then((res) => {
            if (res.data.code === 200) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.init()
            }
          })
        }
      })
    },
    handleExport() { // 导出
      const fieldVal = this.$refs.filterForm.getParms()
      const param = {
        deptCode: fieldVal?.deptCode || '',
        deptName: fieldVal?.deptName || '',
        pdeptCode: fieldVal?.pdeptCode || '',
        pdeptName: fieldVal?.pdeptName || '',
        relationStatus: fieldVal?.relationStatus || '',
        rowStatus: fieldVal?.rowStatus || '',
        ruleCode: this.currentRow.edCode,
        yard: 1
      }
      downloadTemplate(param).then(res => {
        const blob = new Blob([res.data], {
          type: res.data.type + 'charset=utf-8'
        })
        const downloadLink = document.createElement('a')
        const URL = window.URL || window.webkitURL || window.moxURL
        const downUrl = URL.createObjectURL(blob)
        downloadLink.href = downUrl
        downloadLink.download = '科室部门对应.xlsx'
        downloadLink.click()
        window.URL.revokeObjectURL(downUrl)
        // document.body.removeChild(downloadLink) // 删除 a 标签
      })
    },
    handleJc() {
      // 继承
      this.ruleList = Object.assign([], this.deptType).filter((item) => item.edCode !== this.leftRow.edCode)
      this.gzName = ''
      this.jcVisable = true
    },
    handleSaveJc() {
      if (!this.gzName) {
        this.$message.warning('请选择源规则名称')
        return
      }
      const bdOrganization = new BdOrganization()
      bdOrganization.ruleCode = this.leftRow.edCode
      bdOrganization.parentRuleCode = this.gzName
      addInheritanceRules(bdOrganization).then((res) => {
        if (res.data.code === 200) {
          this.$message.success(res.data.message)
          // this.getData()
          this.rowClick(this.currentRow)
          this.jcVisable = false
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    handleSearch(data) {
      this.pageAttr.currentPage = 1
      this.newParamList = data
      this.getData(data)
    },
    pagationChange(payload) {
      this.pageAttr.currentPage = payload.page
      this.pageAttr.pageSize = payload.pageSize
      this.getData(this.newParamList)
      // this.rowClick(this.leftRow, 1);
    },
    // 删除
    deleteDetail(row) {
      this.$confirm({
        content: '是否确认删除？',
        title: '温馨提示',
        okFn: (res) => {
          const ids = [row.id]
          batchDeleteOrganization(ids).then((res) => {
            if (res.data.code === 200) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              // 请求成功后刷新数据
              this.init()
              this.deptFilterM()
            }
          })
        }
      })
    },
    deptCodeFilter(val) {
      getCostDepartments({
        isParent: true,
        projectId: '1',
        deptCode: val,
        isLike: true
      }).then((res) => {
        if (res.data.code === 200) {
          if (res.data.data.length) {
            this.queryForm[2].options = res.data.data
          }
        } else {
          getParentCodeList().then((res) => {
            if (res.data.code === 200) {
              this.queryForm[2].options = res.data.data
            }
          })
        }
      })
    },
    deptFilter(val) {
      getCostDepartments({
        isParent: true,
        projectId: '1',
        deptName: val,
        isLike: true
      }).then((res) => {
        if (res.data.code === 200) {
          if (res.data.data.length) {
            this.queryForm[3].options = res.data.data
          }
        } else {
          getParentCodeList().then((res) => {
            if (res.data.code === 200) {
              this.queryForm[3].options = res.data.data
            }
          })
        }
      })
    },
    deptFilterM() {
      const bdOrganization = new BdOrganization()
      getCostDepartments({
        isParent: true,
        projectId: '1'
      }).then((res) => {
        if (res.data.code === 200) {
          this.parentsList = res.data.data
        }
      })
      bdOrganization.ruleCode = this.edCodeFirst
    },
    unitCodeFilter(val) {
      getCostDepartments({
        isParent: false,
        projectId: '1',
        isLike: true,
        deptCode: val
      }).then((res) => {
        if (res.data.code === 200) {
          if (res.data.data.length) {
            this.queryForm[0].options = res.data.data
          }
        } else {
          getDeptNameList().then((res) => {
            if (res.data.code === 200) {
              this.queryForm[0].options = res.data.data
            }
          })
        }
      })
    },
    unitFilter(val) {
      getCostDepartments({
        isParent: false,
        projectId: '1',
        isLike: true,
        deptName: val
      }).then((res) => {
        if (res.data.code === 200) {
          if (res.data.data.length) {
            this.queryForm[1].options = res.data.data
          }
        } else {
          getDeptNameList().then((res) => {
            if (res.data.code === 200) {
              this.queryForm[1].options = res.data.data
            }
          })
        }
      })
    },
    init() {
      const basicParams = new BasicParams()
      basicParams.edParentCode = 'BD_Organization'
      basicParams.edName = ''
      // basicParams.edEdId = 1;
      basicParams.edExtraFirstCode = ''
      getEnumDetailSource(basicParams).then((res) => {
        if (res.data.code === 200) {
          this.deptType = res.data.data
          let defaultRow = {}
          this.deptType.forEach((item) => {
            if (item.edExtraFirstCode === '1') {
              defaultRow = item
            }
          })
          this.currentRow = defaultRow

          this.$nextTick(() => {
            setTimeout(() => {
              this.rowClick(defaultRow)
            }, 50)
          })
        } else {
          this.loading = false
          this.$message.error(res.data.message)
        }
      })
    },
    addRule() {
      this.dialogTitle2 = '添加规则'
      this.dialogVisible2 = true
    },
    dialogClose() {
      this.isEdit = false
      // this.form = {};
      this.dialogVisible = false
      this.$refs.form.resetFields()
    },
    dialogClose2() {
      this.dialogVisible2 = false
      this.$refs.form2.resetFields()
    },
    dialogCommit() {
      const bdOrganization = new BdOrganization()
      if (this.isEdit) {
        const params = bdOrganization
        params.deptCode = this.form.deptCode.split('/')[0]
        params.deptName = this.form.deptCode.split('/')[1]
        params.pdeptCode = this.form.pdeptCode.split('/')[0]
        params.pdeptName = this.form.pdeptCode.split('/')[1]
        params.ruleCode = this.currentRow.edCode
        params.id = this.pubID
        modifyOrganization(params).then((res) => {
          if (res.data.code === 200) {
            this.$message.success(res.data.message)
            // 这里记得更新页面
            this.dialogVisible = false
            this.$refs.form.resetFields()
            this.rowClick(this.currentRow)
          } else {
            this.$message.error(res.data.message)
          }
        })
      } else {
        if (!this.form.deptCode) {
          this.$message.warning('参数均为必填项！')
          return
        }
        const params = bdOrganization
        params.deptCode = this.form.deptCode.split('/')[0]
        params.deptName = this.form.deptCode.split('/')[1]
        params.pdeptCode = this.form.pdeptCode.split('/')[0]
        params.pdeptName = this.form.pdeptCode.split('/')[1]
        params.ruleCode = this.currentRow.edCode
        addOrganization(params).then((res) => {
          if (res.data.code === 200) {
            this.$message.success(res.data.message)
            this.dialogClose()
            this.rowClick(this.currentRow)
          } else {
            this.$message.error(res.data.message)
          }
        })
      }
    },
    dialogCommit2() {
      // const params = {
      //   edName: this.form2.ruleName
      // }
      const basicParams = new BasicParams()
      if (!this.form2.ruleName) {
        this.$message.warning('规则名称为必填项')
        return
      }
      basicParams.edName = this.form2.ruleName
      basicParams.edParentCode = 'BD_Organization'
      addEnumDetail(basicParams).then((res) => {
        if (res.data.code === 200) {
          this.$message.success(res.data.message)
          this.dialogClose2()
          this.init()
        } else {
          this.$message.error(res.data.message)
        }
      })
      // this.dialogVisible2 = false
    },
    // 获取页面数据
    getData(val) {
      this.loading = true

      const bdOrganization = new BdOrganization()

      if (val) {
        bdOrganization.deptCode = val.deptCode ? val.deptCode : ''
        bdOrganization.deptName = val.deptName ? val.deptName : ''
        bdOrganization.pdeptCode = val.pdeptCode ? val.pdeptCode : ''
        bdOrganization.pdeptName = val.pdeptName ? val.pdeptName : ''
        bdOrganization.rowStatus = val.rowStatus ? val.rowStatus : ''
        bdOrganization.ruleCode =
          this.currentEdCode === '' ? '1' : this.currentEdCode
        bdOrganization.relationStatus = val.relation ? val.relation : ''
      } else {
        bdOrganization.deptCode = ''
        bdOrganization.pdeptCode = ''
        bdOrganization.rowStatus = ''
        bdOrganization.ruleCode = '1'
        bdOrganization.relationStatus = ''
      }
      bdOrganization.pageIndex = this.pageAttr.currentPage
      bdOrganization.pageSize = this.pageAttr.pageSize
      getOrganizationSource(bdOrganization).then((res) => {
        if (res.data.code === 200) {
          this.ruleDetail = res.data.data.list
          this.pageAttr.total = res.data.data.total
          this.loading = false
        } else {
          this.loading = false
          this.$message.error(res.data.message)
        }
      })
    },
    // table - row 点击事件
    rowClick(row, type) {
      this.leftRow = row
      const bdOrganization = new BdOrganization()
      if (row.edExtraFirstCode === '1') {
        this.sureBtnBool = true
      } else {
        this.sureBtnBool = false
      }
      this.currentRow = row
      this.$refs.ruleTable.setCurrentRow(row)
      this.currentEdCode = row.edCode
      this.loading = true
      bdOrganization.ruleCode = row.edCode
      if (type === 1) {
        bdOrganization.pageIndex = this.pageAttr.currentPage
        bdOrganization.currentPage = this.pageAttr.pageSize
      } else {
        this.pageAttr.currentPage = 1
      }
      getOrganizationSource(bdOrganization).then((res) => {
        if (res.data.code === 200) {
          this.ruleDetail = res.data.data.list
          this.pageAttr.total = res.data.data.total
          this.loading = false
        } else {
          this.loading = false
          this.$message.error(res.data.message)
        }
      })
    },
    // 新增按钮事件
    addDetail() {
      const bdOrganization = new BdOrganization()
      this.dialogTitle = '新增对应关系'
      this.deptDisabled = false
      // this.showDialog();
      bdOrganization.ruleCode = this.currentEdCode
      getNoRelationDepartmentList(bdOrganization).then((res) => {
        if (res.data.code === 200) {
          this.deptLists = res.data.data
        }
      })
      this.dialogVisible = true
    },
    // 编辑按钮事件
    editDetail(row) {
      this.isEdit = true
      this.deptDisabled = true
      this.dialogTitle = '修改核算单元'
      this.showDialog(row)
    },
    // 显示新增或编辑 drawer
    showDialog(row) {
      const bdOrganization = new BdOrganization()
      if (this.isEdit) {
        this.pubID = row.id
        bdOrganization.id = row.id
        bdOrganization.ruleCode = this.leftRow.edCode
        bdOrganization.yard = '1'
        getOrganizationNormal(bdOrganization).then((res) => {
          if (res.data.code === 200) {
            if (res.data.data.length === 0) {
              this.$message.error('未找到对应数据')
            } else {
              this.$nextTick(() => {
                this.form.pdeptCode =
                  res.data.data[0].pdeptCode + '/' + res.data.data[0].pdeptName
                this.form.deptCode =
                  res.data.data[0].deptCode + '/' + res.data.data[0].deptName
              })
            }
          }
        })
        // this.$nextTick(() => {
        //   this.form.pdeptCode =
        //     JSON.parse(JSON.stringify(row.pdeptCode)) +
        //     "/" +
        //     JSON.parse(JSON.stringify(row.pdeptName));
        //   this.form.deptCode =
        //     JSON.parse(JSON.stringify(row.deptCode)) +
        //     "/" +
        //     JSON.parse(JSON.stringify(row.deptName));
        // });
      }
      this.dialogVisible = true
    },
    // 开关事件
    switchUse(row) {
      modifyOrganization(row).then((res) => {
        if (res.data.code === 200) {
          this.$message.success(res.data.message)
          this.dialogVisible = false
          this.init()
        } else {
          // row.rowStatus = !row.rowStatus;
          this.$message.error(res.data.message)
        }
      })
    },
    // 向FilterForm中的Select-Options追加选项
    addSeletOption(label, opts) {
      this.queryForm.map((item) => {
        if (item.label === label) {
          item.options = opts
        }
      })
    },
    setDefault(row) {
      const bdOrganization = new BdOrganization()
      if (row.edExtraFirstCode === '1') {
        return
      }
      // const params = {
      //   edId: row.edId,
      //   edExtraFirstCode: "1",
      // };
      row.edExtraFirstCode = '1'
      setDefault(row).then((res) => {
        if (res.data.code === 200) {
          bdOrganization.ruleCode = row.edCode
          getNoRelationDepartmentList(bdOrganization).then((res) => {
            if (res.data.code === 200) {
              this.deptLists = res.data.data
            }
          })
          this.init()
        } else {
          this.$message.error(res.data.data.message)
        }
      })
    },
    reset() {},
    saveEidtRule() {}
  }
}
</script>

<style lang="less" scoped>
.addbox {
  width: 60%;
}
.model-name {
  width: 70px;
  line-height: 39px;
  text-align: right;
  padding-right: 10px;
}
.model-ban {
  margin-top: 15px;
}
// /deep/ .el-dialog__body{
// }
.dept-type {
  padding: 0 !important;
  flex: 0 0 240px;
  margin-right: 10px;
}
.rule-detail {
  padding: 0 !important;
  flex: 1;
}
.title {
  box-sizing: border-box;
  padding: 10px;
  width: 100%;
  background-color: #fefefe;
  border-bottom: 1px solid #e8eaed;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.edit-detail {
  color: #4073ff;
  cursor: pointer;
}
.button-bar {
  height: 48px;
  width: 100%;
  display: flex;
  align-items: center;
  padding-left: 12px;
  // border-bottom: 1px solid #ccc;
  Button:first-of-type {
    margin-right: 12px;
  }
}
.is-child {
  display: inline-block;
  padding-left: 12px;
}
.not-child {
  font-weight: bold;
}
.table-box {
  box-sizing: border-box;
  padding: 12px;
  padding-top: 0;
  height: calc(100% - 130px);
  overflow-y: auto;
}
.query-form {
  // border-bottom: 1px solid #ccc;
  Button {
    margin-left: 12px;
  }
}
.package {
  flex: 0;
  height: 100%;
  width: 78px;
  display: flex;
  justify-content: center;
  align-items: center;
  color: transparent;
}
.is-default {
  color: #4073ff;
}
.set-default {
  margin-right: 4px;
  height: 14px;
  width: 14px;
  border: 1px solid #dcdfe6;
  border-radius: 2px;
  box-sizing: border-box;
  display: flex;
  justify-content: center;
  align-items: center;
  padding-top: 1px;
  cursor: pointer;
  i {
    height: 100%;
    width: 100%;
    color: transparent;
  }
}
.current-default {
  background: #4073ff;
  border: 1px solid #4073ff;
  i {
    color: #fff;
  }
}
.query-box {
  flex: 1;
}
/deep/ .dept-type .el-table--enable-row-hover .el-table__body tr:hover > td {
  .current-default {
    background-color: #fff;
    i {
      color: #217dfa;
    }
  }
  .is-default {
    color: #fff;
  }
  background-color: #217dfa;
  color: #fff;
}
/deep/ .el-table__body tr.current-row > td {
  .current-default {
    background-color: #fff;
    i {
      color: #217dfa;
    }
  }
  .is-default {
    color: #fff;
  }
  background-color: #217dfa;
  color: #fff;
}
</style>

